﻿// Type: Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase
// Assembly: Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: D:\DEV\Gwn.Libraries\Prism4\LIB\Desktop\CommonServiceLocation\Microsoft.Practices.ServiceLocation.dll

using System;
using System.Collections.Generic;
using System.Globalization;
using Gwn.Compatibility;
using Microsoft.Practices.Unity;

namespace Microsoft.Practices.ServiceLocation
{
    public abstract class ServiceLocatorImplBase : IServiceLocator, IServiceProvider
    {
        protected abstract object DoGetInstance(Type serviceType, string key);
        protected abstract IEnumerable<object> DoGetAllInstances(Type serviceType);

        private IUnityContainer _unityContainer;
        protected IUnityContainer Container
        {
            get { return _unityContainer; }
            set { _unityContainer = value; }
        }

        protected ServiceLocatorImplBase() { }

        public void Initialize(IUnityContainer container)
        {
            _unityContainer = container;
        }

        #region IServiceLocator Members

        public virtual TService GetInstance<TService>()
        {
            if (_unityContainer != null)
                return _unityContainer.Resolve<TService>();
            else
                return (TService)this.GetInstance(typeof(TService), null);
        }

        public virtual TService GetInstance<TService>(string key)
        {
            return (TService)this.GetInstance(typeof(TService), key);
        }

        public virtual object GetInstance(Type serviceType) 
        {
            return this.GetInstance(serviceType, null);
        }

        public virtual object GetInstance(Type serviceType, string key)
        {
            object obj2;
            try
            {
                obj2 = this.DoGetInstance(serviceType, key);
            }
            catch (Exception exception)
            {
                throw new ActivationException(this.FormatActivationExceptionMessage(exception, serviceType, key), exception);
            }
            return obj2;
        }

        public virtual object GetService(Type serviceType)
        {
            return this.GetInstance(serviceType, null);
        }


        public virtual IEnumerable<object> GetAllInstances(Type serviceType)
        {
            throw new NotImplementedException("ServiceLocatorImplBase");
        }
        
        public virtual IEnumerable<TService> GetAllInstances<TService>()
        {
            throw new NotImplementedException("ServiceLocatorImplBase");
        }


        #endregion


        protected virtual string FormatActivationExceptionMessage(Exception actualException, Type serviceType, string key)
        {
            return string.Format(CultureInfo.CurrentUICulture, Properties.Resources.ActivationExceptionMessage, new object[] { serviceType.Name, key });
        }

        protected virtual string FormatActivateAllExceptionMessage(Exception actualException, Type serviceType)
        {
            return string.Format(CultureInfo.CurrentUICulture, Properties.Resources.ActivateAllExceptionMessage, new object[] { serviceType.Name });
        }

    }
}
